import json

data = """
{"name": "张三","age": 18,"address": "北京"}
"""

# 将json字符串转化为字典
dct = json.loads(data)
print(dct)

# 将字典转化为json字符串

# 格式化
jsonStr01 = json.dumps(dct, indent=2)
print(jsonStr01)

# 未格式化
jsonStr02 = json.dumps(dct)
print(jsonStr02)

# 写入 JSON 数据
with open('data.json', 'w') as f:
    json.dump(jsonStr01, f)

# 读取数据
with open('data.json', 'r') as f:
    data = json.load(f)
    print(data)
